package com.mlethe.android.demo.fingerprint;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.mlethe.android.demo.R;
import com.mlethe.library.fingerprint.callback.FingerprintCallback;
import com.mlethe.library.fingerprint.FingerprintVerifyManager;
import com.mlethe.library.fingerprint.callback.IFingerprint;
import com.mlethe.library.fingerprint.uitls.FingerprintUtil;

public class MainFingerprintActivity extends AppCompatActivity implements View.OnClickListener {

    private IFingerprint implForNoDialog;
    private FingerprintDialog fingerprintDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fingerprint);
        findViewById(R.id.tvFingerprint).setOnClickListener(this);
        findViewById(R.id.tvFingerprintCustom).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvFingerprint) {
            new FingerprintVerifyManager.Builder(this)
                    .callback(fingerprintCallback)
                    .enableAndroidP(false)
                    .fingerprintColor(ContextCompat.getColor(this, R.color.colorPrimary))
                    .build();
        } else if (id == R.id.tvFingerprintCustom) {
            fingerprintDialog = new FingerprintDialog(this, null)
                    .setActionListener(new FingerprintDialog.OnDialogActionListener() {
                        @Override
                        public void onCancel() {
                            if (implForNoDialog != null) {
                                implForNoDialog.cancel();
                            }
                        }
                    });
            fingerprintDialog.show();
            FingerprintVerifyManager verifyManager = new FingerprintVerifyManager.Builder(this)
                    .callback(fingerprintCallback)
                    .enableAndroidP(false)
                    .fingerprintColor(ContextCompat.getColor(this, R.color.colorPrimary))
                    .build();
            implForNoDialog = verifyManager.getFingerprint();
        }
    }

    private FingerprintCallback fingerprintCallback = new FingerprintCallback() {
        @Override
        public void onSucceeded() {
            if (implForNoDialog != null) {
                fingerprintDialog.cancel();
            }
            Toast.makeText(MainFingerprintActivity.this, getString(R.string.biometricprompt_verify_success), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailed() {
            if (fingerprintDialog != null) {
                fingerprintDialog.setTip(R.string.biometricprompt_verify_failed, com.mlethe.library.fingerprint.R.color.biometricprompt_color_FF5555);
            }
            Toast.makeText(MainFingerprintActivity.this, getString(R.string.biometricprompt_verify_failed), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(String msg) {
            if (fingerprintDialog != null) {
                fingerprintDialog.setTip(msg, com.mlethe.library.fingerprint.R.color.biometricprompt_color_FF5555);
            }
            Log.e("TAG", "onError: "+ msg);
            Toast.makeText(MainFingerprintActivity.this, msg, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel() {
            if (implForNoDialog != null) {
                fingerprintDialog.cancel();
            }
            Toast.makeText(MainFingerprintActivity.this, getString(R.string.fingerprint_cancel), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onHwUnavailable() {
            if (fingerprintDialog != null) {
                fingerprintDialog.cancel();
            }
            Toast.makeText(MainFingerprintActivity.this, getString(R.string.biometricprompt_finger_hw_unavailable), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNoneEnrolled() {
            //弹出提示框，跳转指纹添加页面
            AlertDialog.Builder lertDialogBuilder = new AlertDialog.Builder(MainFingerprintActivity.this);
            lertDialogBuilder.setTitle(getString(R.string.biometricprompt_tip))
                    .setMessage(getString(R.string.biometricprompt_finger_add))
                    .setCancelable(false)
                    .setNegativeButton(getString(R.string.biometricprompt_finger_add_confirm), ((DialogInterface dialog, int which) -> {
                        FingerprintUtil.startFingerprint(MainFingerprintActivity.this);
                    }
                    ))
                    .setPositiveButton(getString(R.string.biometricprompt_cancel), ((DialogInterface dialog, int which) -> {
                        dialog.dismiss();
                    }
                    ))
                    .create().show();
        }

    };
}
